<?php
/*
 * @分销商城
 * @功能说明：商品管理中心
 * @更新说明：暂无更新
 * @文件名 GoodsController.class.php
 * @编码 UTF-8
 * @创建时间 2015-12-25 上午11:50:21
 * @创建人 XING
 */
namespace hb\Controller;
use Think\Controller;
class GoodsController extends ApibaseController {
	/*
	 * 分销商城_商品管理
	 * @date: 2015-12-25 上午11:50:41
	 * @editor: XING
	 */
    public function index(){
    	/*参数初始化*/
		exit;
    	$page = I('get.page') ?: 1;
    	$perpage = 10;
    	$list = M('GoodsEntity')->where(array('resource'=>'0','delete_at'=>'0'));
    	$url = array();
    	if(I('get.goods_title')){
    		$list = $list->where('goods_title LIKE "%'.I('get.goods_title').'%"');
    		$url['goods_title'] = I('get.goods_title');
    	}
		if(I('get.goods_skuid')){
			$list = $list->where(array('goods_skuid'=>I('get.goods_skuid')));
			$url['goods_skuid'] = I('get.goods_skuid');
		}
    	$sObject = clone $list;
    	$getData = $list->order(array('add_at'=>'desc'))->page($page,$perpage)->select();
    	$this->assign('page',setPage($sObject->count(),$perpage,$page,'?'.http_build_query($url)));
    	$this->assign('getData',$getData);

    	if(I('get.ref'))
    		$this->display('goods/sindex');    		
    	else
    		$this->display('goods/index');
    }

	/*
	 * 分销商城_商品在线管理
	 * @date: 2016-04-27 10:37:00
	 * @editor: WENCUN
	 * */
	public function online(){
		$page = I('get.page')?:1;
		$perpage = 10;
		$url = array();
		$list = M('GoodsEntity')->where(array('status'=>'1','resource'=>'0'));
		if(I('get.goods_title')){
			$list = $list->where("goods_title LIKE '%".I('get.goods_title')."%'");
			$url['goods_title'] = I('get.goods_title');
		}
		$object = clone $list;
		$getData = $list->order(array('add_at'=>'DESC'))->page($page,$perpage)->select();
		$this->assign('page',setPage($object->count(),$perpage,$page,'?'.http_build_query($url)));
		$this->assign('getData',$getData);
		$this->display('/goods/online');
	}

	/*
	 * 分销商城_仓库更新在线商品库存
	 * @date: 2016-04-27 11:25:00
	 * @editor: WENCUN
	 * */
	public function setKcun(){
		if(IS_POST){
			if(I('goods_id') && I('dosubmit')){
				$setKcun = M('GoodsEntity')->where(array('goods_id'=>I('post.goods_id')))->setField(array(
					'kcun' => I('post.kcun')
				));
				if($setKcun){
					$this->showtips('在线商品库存更新成功');
				}else{
					$this->showtips('操作失败');
				}
			}
		}
		if(I('get.goods_id')){
			$getData = M('GoodsEntity')->where(array('resource'=>'0','goods_id'=>I('get.goods_id')))->find();
			$this->assign('getData',$getData);
		}
		$this->display('/goods/setkcun');
	}
    
	/*
	 * 分销商城_添加商品
	 * @date: 2016-04-25 10:16:16
	 * @editor: XING
	 */
    public function addGoods(){
    	if(I('post.dosubmit')){
    		if(I('post.goods_title') == ''){
    			$this->showtips('商品名称有误');
    		}
    		if(I('post.goods_price') <= '0'){
    			$this->showtips('商品价格有误');
    		}
    		if(I('post.goods_attr') <= '0'){
    			$this->showtips('商品规格有误');
    		}
    		if(I('post.goods_id')){
				$upData = array(
					'goods_title' => trim(I('post.goods_title')),
					'goods_skuid' => trim(I('post.goods_skuid')),
					'goods_price' => trim(I('post.goods_price')),
					'kcun' => trim(I('post.kcun')),
					'brand' => trim(I('post.brand')),
					'sort' => trim(I('post.sort')),
					'goods_pic' => trim(I('post.goods_pic')),
					'goods_pic2' => trim(I('post.goods_pic2')),
					'goods_pic3' => trim(I('post.goods_pic3')),
					'goods_attr' => trim(I('post.goods_attr')),
					'goods_content' => htmlspecialchars(I('post.content')),
					'update_at' => time()
				);
    			$effectId = M('GoodsBjie')->where(array('goods_id'=>I('post.goods_id')))
	    				  ->setField($upData);
    			if($effectId){
					//putlog('更新商品 `'.I('post.goods_title').'`','GoodsController','addGoods');
					$this->showtips('商品信息更新成功');
    			}else{
    				$this->showtips('操作失败');
    			}
    		}
			$data = array(
				'goods_title' => trim(I('post.goods_title')),
				'goods_skuid' => trim(I('post.goods_skuid')),
				'goods_price' => trim(I('post.goods_price')),
				'kcun' => trim(I('post.kcun')),
				'brand' => trim(I('post.brand')),
				'sort' => trim(I('post.sort')),
				'goods_pic' => trim(I('post.goods_pic')),
				'goods_pic2' => trim(I('post.goods_pic2')),
				'goods_pic3' => trim(I('post.goods_pic3')),
				'goods_attr' => trim(I('post.goods_attr')),
				'goods_content' => htmlspecialchars(I('post.content')),
				'add_at' => time(),
				'update_at' => time()
			);
    		$addId = M('GoodsBjie')->add($data);
    		if($addId){
				//putlog('入库商品 '.I('post.goods_title').' ID`'.$addId.'`','GoodsController','addGoods');
				$this->showtips('入库商品成功');
    		}else{
				M('GoodsBjie')->rollback();
    			$this->showtips('操作失败');
    		}
    	}
    	$getLevels = M('CateEntity')
    			   ->where(array('parent_id'=>'0'))->select();
    	$this->assign('cate',$getLevels);
    	if(I('get.goods_id')){
    		$getData = M('GoodsEntity')
    				 ->where(array('goods_id'=>I('get.goods_id'),'resource'=>'0'))->find();
    		$this->assign('getData',$getData);
    	}
    	$this->display('goods/addgoods');
    }

    
	/*
	 * 分销商城_分类AJAX
	 * @date: 2015-12-25 下午1:21:34
	 * @editor: XING
	 */
    public function cates(){
    	if(I('get.ajaxFrom')){
    		$cates = M('CateEntity')->where(array(
    				'parent_id' => I('get.cateid')
    			))->order(array('sort'=>'desc'))->select();
    	
    		jsonRespons($cates ?: 0);
    	}
    }
    
    /*
     * 分销商城_查询价格
     * @date: 2016-3-25 下午7:53:33
     * @editor: XING
     */
    public function gprice(){
    	if(I('get.price')){
    		$arrProfit = explode(',',getSysConfig('levelRate'));
    		echo json_encode(array('price1'=>$arrProfit[0]*I('get.price'),'price2'=>$arrProfit[1]*I('get.price'),'price3'=>$arrProfit[2]*I('get.price')));exit;
    	}
    }
    
}